﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Order;
using Kevin.Shop.Business.SalesManagement.Components.Order;
using Kevin.Shop.Business.Comments.Components;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;

namespace RiderShop.Mobile.Modle.MyAccount
{
    public class MyCommentModel
    {
        /// <summary>
        /// 用户的收藏列表集合
        /// </summary>
        public List<OrderCustomerReviewInfo> CommentList { get; set; }

        /// <summary>
        /// 商品评价列表
        /// </summary>
        public List<ProductReviewInfoExtend> ProductCommentList { get; set; }

        #region Get User Comment
        /// <summary>
        /// 得到用户的收藏列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页得到多少条</param>
        /// <returns></returns>
        public DbQueryResult<List<OrderCustomerReviewInfo>> GetUserComments(int pageIndex, int pageSize)
        {
            string url = SiteUrl.FormatUrl("SalesManagement_GetUserReviewProductList");
            var query = new OrderQueryBasicInfo()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                InUser = WebClientContext.Current.UserId
            };

            DbQueryResult<List<OrderCustomerReviewInfo>> result = RestfulHelper.Query<OrderCustomerReviewInfo>(url, query);

            TotalCount = result.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)pageSize);
            PageIndex = pageIndex;
            PageSize = pageSize;

            return result;
        }

        public DbQueryResult<List<OrderCustomerReviewInfo>> GetUserUnReviewComments(int pageIndex, int pageSize)
        {
            string url = SiteUrl.FormatUrl("SalesManagement_GetUserUnReviewProductList");
            var query = new OrderQueryBasicInfo()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                InUser = WebClientContext.Current.UserId
            };

            DbQueryResult<List<OrderCustomerReviewInfo>> result = RestfulHelper.Query<OrderCustomerReviewInfo>(url, query);

            TotalCount = result.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)pageSize);
            PageIndex = pageIndex;
            PageSize = pageSize;

            return result;
        }
        #endregion

        #region Get Product Comment List
        public DbQueryResult<List<ProductReviewInfoExtend>> GetProductReview(string productId, int pageIndex, int pageSize, ProductReviewType reviewType)
        {
            var query = new ProductReviewAndReplyQuery();
            query.ProductId = productId;
            query.PageIndex = pageIndex;
            query.PageSize = pageSize;
            query.Rating = reviewType;
            DbQueryResult<List<ProductReviewInfoExtend>> productReviewList = RestfulHelper.Query<ProductReviewInfoExtend>(SiteUrl.FormatUrl("ProductManagement_ProductReviewGet"), query);

            ProductCommentList = productReviewList.Data;

            TotalCount = productReviewList.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)pageSize);
            PageIndex = pageIndex;
            PageSize = pageSize;
            ReviewType = (int)reviewType;
            return productReviewList;
        }
        #endregion

        #region Create Product Comment
        public ServiceResponser CreateProductComment(ProductReviewInfo comment)
        {
            string url = SiteUrl.FormatUrl("ProductManagement_ProductReviewUpdate");
            comment.CurrentUserId = WebClientContext.Current.UserId;
            comment.MeritText = comment.MeritText == null ? string.Empty : comment.MeritText;
            comment.FaultsText = comment.FaultsText == null ? string.Empty : comment.FaultsText;
            comment.ImproveText = comment.ImproveText == null ? string.Empty : comment.ImproveText;

            var response = RestfulHelper.Update(url, comment);

            return response;
        }
        #endregion

        //页面索引值
        private int _pageIndex;
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        //每页记录的数量
        private int _pageSize;
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        //总记录数
        private int _totalCount;
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }

        //总页数
        private int _totalPage;
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }
        /// <summary>
        /// ReviewType
        /// </summary>
        public int ReviewType { get; set; }
    }
}
